//
//  CQPlaceholerTextView.swift
//  weibo
//
//  Created by chengqiang on 16/7/29.
//  Copyright © 2016年 chengqiangBiaodashi. All rights reserved.
//

import UIKit

class CQPlaceholerTextView: UITextView {
    var placehodler :String?{
        didSet{
            
        
        placeholerLable.text = placehodler
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
        //onserver：谁来监听
        //selrctor：监听方法
        //name：监听同志的名字
        //object：同志的发送着 ， nil 指都能接受
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textViewDidChange", name: UITextViewTextDidChangeNotification, object: self)
    }
    //添加控件
    private func setupUI(){
        
        self.addSubview(placeholerLable)
        placeholerLable.translatesAutoresizingMaskIntoConstraints = false
        //添加约束
        placeholerLable.translatesAutoresizingMaskIntoConstraints = false
        self.addConstraint(NSLayoutConstraint(item: placeholerLable, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 5))
        
        self.addConstraint(NSLayoutConstraint(item: placeholerLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 8))
    }
    
    //文字改变后调用
    func textViewDidChange()  {
        placeholerLable.hidden = self.hasText()
    }
    
    //移除通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
 
    
    
    //占位Lable
    private lazy var placeholerLable: UILabel = {
       let lable = UILabel ()
        lable.font = UIFont.systemFontOfSize(18)
        lable.textColor = UIColor.lightGrayColor()
        lable.text = "请输入内容..."
        return lable
    }()
    
}
